package commands;

import java.util.HashMap;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.event.Listener;

import persistence.CSubPlugin;

public class CCommandHandler implements Listener, CommandExecutor {
	CSubPlugin parent;
	private CCommandLoader cmdList;

	public boolean addCommand(ICommand cmd) {
		if (cmdList.containsKey(cmd.getCommandName())) {
			return false;
		} else {
			if (cmdList.containsValue(cmd)) {
				return false;
			}
		}
		cmdList.put(cmd.getCommandName(), cmd);
		return true;
	}

	public CCommandHandler(CSubPlugin parentSub) {
		parent = parentSub;
		cmdList = new CCommandLoader(this);  
	}

	public ICommand getCmdByName(String name) {
		return cmdList.get(name);
	}

	public HashMap<String, ICommand> getAllCmd() {
		return cmdList;
	}

	@Override
	public boolean onCommand(CommandSender sender, Command command,
			String label, String[] args) {
		if(!cmdList.containsKey(command.getName()))
		{
			for (ICommand cmd : cmdList.values()) {
				if(cmd.isCommandAccepted(label)) return cmd.exec(sender, args);
			}
		}
		return cmdList.get(command.getName()).exec(sender, args);
	}

}
